﻿using System;

namespace YArchitech.Plumbing
{
	public class HYPumpOperationCurveDataBase : FitCurve
	{
		public HYPumpOperationCurveDataBase()
		{
		}

		public HYPumpOperationCurveDataBase(int nTimes) : base(nTimes)
		{
		}

		public virtual void CalulateExtents()
		{
		}

		public virtual bool GetQbyValue(double dValue, ref double dQ, double dLeftQBound = -1.0, double dRightBound = -1.0)
		{
			double num = (dLeftQBound > 0.0) ? dLeftQBound : this.m_curveLeftPt.first;
			double num2 = (dRightBound > 0.0) ? dRightBound : this.m_curveRightPt.first;
			double num3 = (num2 - num) * 0.5;
			num2 += num3;
			double num4 = base.CalCurveValue(num) - dValue;
			double num5 = base.CalCurveValue(num2) - dValue;
			if (Math.Abs(num4) < ConstantValue.ITOR_TOLERANCEVALUE)
			{
				dQ = num;
				return true;
			}
			if (Math.Abs(num5) < ConstantValue.ITOR_TOLERANCEVALUE)
			{
				dQ = num2;
				return true;
			}
			if (Math.Abs(num2 - num) < ConstantValue.ITOR_TOLERANCERANGE * 1.5)
			{
				return false;
			}
			double num6 = 0.5 * (num + num2);
			int i = 0;
			while (i < ConstantValue.ITOR_MAXCOUNT)
			{
				double num7 = base.CalCurveValue(num6) - dValue;
				if (Math.Abs(num7) < ConstantValue.ITOR_TOLERANCEVALUE)
				{
					dQ = num6;
					return true;
				}
				if (num7 * num4 < 0.0)
				{
					num2 = num6;
					num5 = base.CalCurveValue(num2) - dValue;
				}
				else if (num7 * num5 < 0.0)
				{
					num = num6;
					num4 = base.CalCurveValue(num) - dValue;
				}
				else
				{
					if (Math.Abs(num - num2) < ConstantValue.ITOR_TOLERANCERANGE)
					{
						return false;
					}
					if (Math.Abs(num) < ConstantValue.ITOR_TOLERANCERANGE && Math.Abs(num4) < 0.2)
					{
						dQ = num;
						return true;
					}
					double num8 = 0.0;
					if (!this.GetQByDerivedValue(0.0, ref num8, num, num2))
					{
						return false;
					}
					if (num8 < 0.0)
					{
						return false;
					}
					if (Math.Abs(num2 - (num8 + num3)) < ConstantValue.ITOR_TOLERANCERANGE || Math.Abs(num2 - (num8 + num3)) < ConstantValue.ITOR_TOLERANCERANGE)
					{
						return false;
					}
					num2 = num8;
					return this.GetQbyValue(dValue, ref dQ, num, num2);
				}
				if (Math.Abs(num - num2) < ConstantValue.ITOR_TOLERANCERANGE)
				{
					return true;
				}
				i++;
				num6 = 0.5 * (num + num2);
			}
			return false;
		}

		public virtual bool GetMaxQandValue(ref double dQ, ref double dValue)
		{
			dQ = this.m_curveRightPt.first;
			dValue = this.m_curveRightPt.second;
			return true;
		}

		public virtual bool GetMinQandValue(ref double dQ, ref double dValue)
		{
			dQ = this.m_curveLeftPt.first;
			dValue = this.m_curveLeftPt.second;
			return true;
		}

		public virtual bool GetMaxValue(ref double dValue, ref double dQ)
		{
			dQ = this.m_curveHighPt.first;
			dValue = this.m_curveHighPt.second;
			return true;
		}

		public virtual bool GetMinValue(ref double dValue, ref double dQ)
		{
			dValue = this.m_curveLowPt.second;
			dQ = this.m_curveLowPt.first;
			return true;
		}

		public virtual bool GetQByDerivedValue(double dDerivedValue, ref double dQ, double dLeftBound = -1.0, double dRightBound = -1.0)
		{
			double num = (dLeftBound > 0.0) ? dLeftBound : this.m_curveLeftPt.first;
			double num2 = (dRightBound > 0.0) ? dRightBound : this.m_curveRightPt.first;
			if (this.m_nTimes == 1)
			{
				if (this.m_dDerivedParam[0] != dDerivedValue)
				{
					return false;
				}
				dQ = num2;
				return true;
			}
			else if (this.m_nTimes == 2)
			{
				double num3 = (dDerivedValue - this.m_dDerivedParam[0]) / this.m_dDerivedParam[1];
				if (num3 >= num && num3 <= num2)
				{
					dQ = num3;
					return true;
				}
				return false;
			}
			else
			{
				double num4 = base.CalDeriveValue(num) - dDerivedValue;
				double num5 = base.CalDeriveValue(num2) - dDerivedValue;
				if (Math.Abs(num4) < ConstantValue.ITOR_TOLERANCEVALUE)
				{
					dQ = num;
					return true;
				}
				if (Math.Abs(num5) < ConstantValue.ITOR_TOLERANCEVALUE)
				{
					dQ = num2;
					return true;
				}
				double num6 = 0.5 * (num + num2);
				int i = 0;
				while (i < ConstantValue.ITOR_MAXCOUNT)
				{
					double num7 = base.CalDeriveValue(num6) - dDerivedValue;
					if (Math.Abs(num7) < ConstantValue.ITOR_TOLERANCEVALUE)
					{
						dQ = num6;
						return true;
					}
					if (num7 * num4 < 0.0)
					{
						num2 = num6;
						num5 = base.CalDeriveValue(num2) - dDerivedValue;
					}
					else
					{
						if (num7 * num5 >= 0.0)
						{
							return false;
						}
						num = num6;
						num4 = base.CalDeriveValue(num) - dDerivedValue;
					}
					if (Math.Abs(num - num2) < ConstantValue.ITOR_TOLERANCERANGE)
					{
						return true;
					}
					i++;
					num6 = 0.5 * (num + num2);
				}
				return false;
			}
		}

		public stCurvePt m_curveLowPt = new stCurvePt();

		public stCurvePt m_curveHighPt = new stCurvePt();

		public stCurvePt m_curveLeftPt = new stCurvePt();

		public stCurvePt m_curveRightPt = new stCurvePt();
	}
}
